<?php

/**
* Class to provide methods for handling customer companies type
*
* @author Nizar Oukhchi
* @name Type
* @package CollaBTP
*/
class type {
    private $mylog;

    /**
    * Constructor
    * Initializes the event log
    */
    function __construct()
    {
        $this->mylog = new mylog;
    }

    /**
    * Add a type
    *
    * @param array $data
    * @return int $insid ID of the inserted type
    */
    function add($data)
    {
        global $conn;

        $ins1Stmt = $conn->prepare("INSERT INTO type (`typename`, `typedesc` VALUES (?,?)");
        $ins1 = $ins1Stmt->execute(array($data['name'] ,$data['desc']));
        $insid = $conn->lastInsertId();

        if ($ins1) {
            return $insid;
        } else {
            return false;
        }
    }

    /**
    * Edit a type
    *
    * @param array $data Type data
    * @return bool
    */
    function edit($data)
    {
        global $conn;

        $id = (int) $data['id'];

        $updStmt = $conn->prepare("UPDATE company SET `company`=?, `contact`=?, `email`=?, `phone`=?, `mobile`=?, `url`=?, `address`=?, `zip`=?, `city`=?, `country`=?, `state`=?, `desc`=? WHERE ID = ?");
        $upd = $updStmt->execute(array($data['company'], $data['contact'], $data['email'], $data['phone'], $data['mobile'], $data['url'], $data['address'], $data['zip'], $data['city'], $data['country'], $data['state'], $data['desc'], $id));

        if ($upd) {
            return true;
        } else {
            return false;
        }
    }

    /**
    * Delete a type
    *
    * @param int $id Type ID
    * @return bool
    */
    function del($id)
    {
        global $conn;

        $id = (int) $id;

        $del_assigns = $conn->query("DELETE FROM company_assigned WHERE customer = $id");
        $del = $conn->query("DELETE FROM customer WHERE ID = $id");

        if ($del) {
            return true;
        } else {
            return false;
        }
    }


    /**
    * Get a type
    *
    * @param int $id Type ID
    * @return array $type Type
    */
    function getType($id)
    {
        global $conn;

        $id = (int) $id;

        $sel = $conn->prepare("SELECT * FROM company WHERE ID = ?");
        $selStmt = $sel->execute(array($id));

        $company = $sel->fetch();

        if (!empty($company)) {
            return $company;
        } else {
            return false;
        }
    }

    /**
    * Get a list of all types
    *
    * @return array $types List of all types
    */
    function getAllTypes()
    {
        global $conn;

        $sel = $conn->query("SELECT * FROM type");
        $types = array();

        while ($type = $sel->fetch()) {
            array_push($types, $type);
        }

        if (!empty($types)) {
            return $types;
        }else {
            return false;
        }
    }
}

?>
